package collection;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @author Cheng Jun
 * Description: 请输入描述
 * @version 1.0
 * @date 2020/8/6 11:12
 */
public class FailFastTest {

    public static List<String> list = new ArrayList<>();

    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            list.add(i + "");
        }
        MyThread1 thread1 = new MyThread1();
        thread1.setName("thread1");
        thread1.start();
        MyThread2 thread2 = new MyThread2();
        thread2.setName("thread2");
        thread2.start();
    }

    private static class MyThread1 extends Thread {
        @Override
        public void run() {
            Iterator<String> iterator = list.iterator();
            while (iterator.hasNext()) {
                String s = iterator.next();
                System.out.println(this.getName() + ":" + s);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            super.run();
        }
    }

    private static class MyThread2 extends Thread {
        int i = 0;

        @Override
        public void run() {
            while (i < 10) {
                System.out.println("thread2:" + i);
                if (i == 2) {
                    list.set(i, "xiugai");
                }
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                i++;
            }
        }
    }

}
